!function(a) {
    a.fn.dragSort = function(b) {
        var c = a.extend({}, a.fn.dragSort.defaults, b), d = new Array(), e = null, f = null;
        return this.selector && this.each(function(b, g) {
            a(g).is("table") && 1 == a(g).children().size() && a(g).children().is("tbody") && (g = a(g).children().get(0));
            var h = {
                draggedItem: null,
                placeHolderItem: null,
                pos: null,
                offset: null,
                offsetLimit: null,
                container: g,
                init: function() {
                    a(this.container).attr("listIdx", b).mousedown(this.grabItem).find(c.dragSelector);
                },
                grabItem: function(b) {
                    var f, g, h, i;
                    if (2 != b.button && !a(b.target).is(c.dragSelectorExclude)) {
                        for (f = b.target; !a(f).is("[listIdx=" + a(this).attr("listIdx") + "] " + c.dragSelector); ) {
                            if (f == this) return;
                            f = f.parentNode;
                        }
                        return null != e && null != e.draggedItem && e.dropItem(), a(b.target).css("cursor", "move"), 
                        e = d[a(this).attr("listIdx")], e.draggedItem = a(f).closest(c.itemSelector), g = parseInt(e.draggedItem.css("marginTop")), 
                        h = parseInt(e.draggedItem.css("marginLeft")), e.offset = e.draggedItem.offset(), 
                        e.offset.top = b.pageY - e.offset.top + (isNaN(g) ? 0 : g) - 1, e.offset.left = b.pageX - e.offset.left + (isNaN(h) ? 0 : h) - 1, 
                        c.dragBetween || (i = 0 == a(e.container).outerHeight() ? Math.max(1, Math.round(.5 + a(e.container).children(c.itemSelector).size() * e.draggedItem.outerWidth() / a(e.container).outerWidth())) * e.draggedItem.outerHeight() : a(e.container).outerHeight(), 
                        e.offsetLimit = a(e.container).offset(), e.offsetLimit.right = e.offsetLimit.left + a(e.container).outerWidth() - e.draggedItem.outerWidth(), 
                        e.offsetLimit.bottom = e.offsetLimit.top + i - e.draggedItem.outerHeight()), e.draggedItem.css({
                            position: "absolute",
                            opacity: .8,
                            "z-index": 999
                        }).after(c.placeHolderTemplate), e.placeHolderItem = e.draggedItem.next().css("height", e.draggedItem.height()).attr("placeHolder", !0), 
                        a(d).each(function(a, b) {
                            b.ensureNotEmpty(), b.buildPositionTable();
                        }), e.setPos(b.pageX, b.pageY), a(document).bind("selectstart", e.stopBubble), a(document).bind("mousemove", e.swapItems), 
                        a(document).bind("mouseup", e.dropItem), !1;
                    }
                },
                setPos: function(b, d) {
                    var e = d - this.offset.top, f = b - this.offset.left;
                    c.dragBetween || (e = Math.min(this.offsetLimit.bottom, Math.max(e, this.offsetLimit.top)), 
                    f = Math.min(this.offsetLimit.right, Math.max(f, this.offsetLimit.left))), this.draggedItem.parents().each(function() {
                        if ("static" != a(this).css("position") && (!a.browser.mozilla || "table" != a(this).css("display"))) {
                            var b = a(this).offset();
                            return e -= b.top, f -= b.left, !1;
                        }
                    }), this.draggedItem.css({
                        top: e,
                        left: f
                    });
                },
                buildPositionTable: function() {
                    var b = null == this.draggedItem ? null : this.draggedItem.get(0), d = new Array();
                    a(this.container).children(c.itemSelector).each(function(c, e) {
                        if (e != b) {
                            var f = a(e).offset();
                            f.right = f.left + a(e).width(), f.bottom = f.top + a(e).height(), f.elm = e, d.push(f);
                        }
                    }), this.pos = d;
                },
                dropItem: function() {
                    return null != e.draggedItem ? (e.placeHolderItem.before(e.draggedItem), e.draggedItem.css({
                        position: "",
                        top: "",
                        left: "",
                        opacity: "",
                        "z-index": ""
                    }), e.placeHolderItem.remove(), a("*[emptyPlaceHolder]").remove(), c.dragEnd.apply(e.draggedItem), 
                    e.draggedItem = null, a(document).unbind("selectstart", e.stopBubble), a(document).unbind("mousemove", e.swapItems), 
                    a(document).unbind("mouseup", e.dropItem), !1) : void 0;
                },
                stopBubble: function() {
                    return !1;
                },
                swapItems: function(b) {
                    var g, h, i;
                    if (null == e.draggedItem) return !1;
                    for (e.setPos(b.pageX, b.pageY), g = e.findPos(b.pageX, b.pageY), h = e, i = 0; -1 == g && c.dragBetween && i < d.length; i++) g = d[i].findPos(b.pageX, b.pageY), 
                    h = d[i];
                    return -1 == g || a(h.pos[g].elm).attr("placeHolder") ? !1 : (null == f || f.top > e.draggedItem.offset().top || f.left > e.draggedItem.offset().left ? a(h.pos[g].elm).before(e.placeHolderItem) : a(h.pos[g].elm).after(e.placeHolderItem), 
                    a(d).each(function(a, b) {
                        b.ensureNotEmpty(), b.buildPositionTable();
                    }), f = e.draggedItem.offset(), !1);
                },
                findPos: function(a, b) {
                    for (var c = 0; c < this.pos.length; c++) if (this.pos[c].left < a && this.pos[c].right > a && this.pos[c].top < b && this.pos[c].bottom > b) return c;
                    return -1;
                },
                ensureNotEmpty: function() {
                    var b, d, e;
                    c.dragBetween && (b = null == this.draggedItem ? null : this.draggedItem.get(0), 
                    d = null, e = !0, a(this.container).children(c.itemSelector).each(function(c, f) {
                        a(f).attr("emptyPlaceHolder") ? d = f : f != b && (e = !1);
                    }), e && null == d ? a(this.container).append(c.placeHolderTemplate).children(":last").attr("emptyPlaceHolder", !0) : e || null == d || a(d).remove());
                }
            };
            h.init(), d.push(h);
        }), this;
    }, a.fn.dragSort.defaults = {
        itemSelector: "li",
        dragSelector: "li",
        dragSelectorExclude: "input, a[href]",
        dragEnd: function() {},
        dragBetween: !1,
        placeHolderTemplate: "<li>&nbsp;</li>"
    };
}(jQuery);